class_name Tooltip
extends PanelContainer

@export var fadeTime:=0.2

@onready var desc:RichTextLabel=%Desc
@onready var icon:TextureRect=%Icon

var tween:Tween
var isVisible=false

func _ready():
	modulate.a=0
	hide()

	Events.showTip.connect(showTip)
	Events.hideTip.connect(hideTip)

func showTip(texture:Texture,text:String)->void:
	isVisible=true
	if tween:tween.kill()

	icon.texture=texture
	desc.text=text

	tween=create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.tween_callback(show)
	tween.tween_property(self,"modulate:a",1,fadeTime)

func hideTip()->void:
	isVisible=false
	if tween:tween.kill()
	get_tree().create_timer(fadeTime,false).timeout.connect(hideAnimation)

func hideAnimation()->void:
	if isVisible:return
	tween=create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.tween_property(self,"modulate:a",0,fadeTime)
	tween.tween_callback(hide)

